package org.pinda.gui;

import static org.johnnei.engine.render.RenderObject.VERTEX_COLOR;

import org.johnnei.engine.GameMouse;
import org.johnnei.engine.render.ColorHelper;
import org.johnnei.engine.render.TextRenderer;
import org.johnnei.engine.render.VertexHelper;

public class GuiButton extends GuiComponent {
	
	public static final int PADDING = 5;
	public static final int TOTAL_PADDING = 2 * PADDING;
	
	private boolean isClicked = false;
	
	private boolean centered;
	private String text;
	private ColorHelper textColor;
	private ColorHelper buttonColor;
	
	/**
	 * Creates a button on the center of the screen (x alignment)
	 * @param centered
	 * @param text
	 * @param buttonCollor
	 * @param color
	 */
	public GuiButton(Gui parent, int y, String text, ColorHelper buttonColor, ColorHelper textColor) {
		super(parent, VERTEX_COLOR);
		width = TOTAL_PADDING + TextRenderer.getTextWidth(text);
		height = TOTAL_PADDING + TextRenderer.getTextHeight();
		this.x = (parent.width / 2) - (width / 2);
		this.y = y;
		this.centered = false;
		this.text = text;
		this.textColor = textColor;
		this.buttonColor = buttonColor;
		generateVertexData();
		generateColorData();
	}
	
	public GuiButton(Gui parent, int y, int width, boolean centered, String text, ColorHelper buttonColor, ColorHelper textColor) {
		super(parent, VERTEX_COLOR);
		this.width = width;
		this.height = TOTAL_PADDING + TextRenderer.getTextHeight();
		this.x = (parent.width / 2) - (width / 2);
		this.y = y;
		this.centered = centered;
		this.text = text;
		this.textColor = textColor;
		this.buttonColor = buttonColor;
		generateVertexData();
		generateColorData();
	}
	
	public GuiButton(Gui parent, float x, float y, float width, float height, boolean centered, String text, ColorHelper buttonColor, ColorHelper textColor) {
		super(parent, VERTEX_COLOR);
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.centered = centered;
		this.text = text;
		this.buttonColor = buttonColor;
		this.textColor = textColor;
		generateVertexData();
		generateColorData();
	}
	
	public GuiButton(Gui parent, int x, int y, int height, boolean centered, String text, ColorHelper buttonColor, ColorHelper textColor) {
		super(parent, VERTEX_COLOR);
		this.x = x;
		this.y = y;
		this.width = TOTAL_PADDING + TextRenderer.getTextWidth(text);
		this.height = height;
		this.centered = centered;
		this.text = text;
		this.buttonColor = buttonColor;
		this.textColor = textColor;
		generateVertexData();
		generateColorData();
	}
	
	/**
	 * Checks if the mouse is clicked within the borders of this button
	 * @return true if the mouse is clicked on this button
	 */
	public boolean isClicked() {
		boolean isClicked = this.isClicked;
		this.isClicked = false;
		return isClicked;
	}
	
	@Override
	public void onClick(int mouseButton, int x, int y) {
		if(mouseButton == GameMouse.LEFT_BUTTON) 
			isClicked = true;
	}
	
	@Override
	public void generateVertexData() {
		VertexHelper vertex = new VertexHelper(2 * 4);
		vertex.put(x, y, width, height);
		renderObject.updateVertex(vertex);
	}
	
	@Override
	public void generateColorData() {
		renderObject.updateColor(buttonColor);
	}
	
	@Override
	public void render() {
		super.render();
		if(centered)
			TextRenderer.drawCentered(getCenterX(), y + PADDING, text, textColor);
		else
			TextRenderer.draw(x + PADDING, y + PADDING, text, textColor);
	}

}
